package operation;

import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.tester.JListTester;
import com.supermap.data.Datasource;
import com.supermap.data.DatasourceConnectionInfo;
import com.supermap.data.EngineType;
import com.supermap.desktop.core.Application;
import robotCross.TextMatcher;
import robotCross.TipMatcher;
import util.Info;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

public class OperationUtil {

	/**
	 * 根据文本查找下一个空间，用于目标空间本身无法有text等属性查找时
	 * @param text
	 * @return
	 */
	public static Component findNextComponentByText(JComponent parentComponent ,String text){
		Component component = null;
		if (parentComponent == null || text == null)
			return component;
		for (int i = 0; i < parentComponent.getComponents().length; i++) {
			if (parentComponent.getComponent(i) instanceof JLabel) {
				if (text.equals(((JLabel) parentComponent.getComponent(i)).getText())
						&& parentComponent.getComponent(i).isShowing() == true) {
					if (i < parentComponent.getComponents().length - 1) {
						return parentComponent.getComponent(i + 1);
					}
				}
			} else if (parentComponent.getComponentCount() > 0
					&& parentComponent.getComponent(i) instanceof JComponent) {
				component = findNextComponentByText((JComponent) parentComponent.getComponent(i),
						text);
				if (component != null) {
					return component;
				}
			}
		}
		return component;
	}

	/* 按照层级在指定控件上查找控件 */
	public static Component searchComponentByMap(Component c,
			ArrayList<SearchMap> mapList) {
		Component findCmp = c;
		Iterator<SearchMap> iterator = mapList.iterator();
		while (iterator.hasNext()) {
			SearchMap map = iterator.next();
			findCmp = findComponent(findCmp, map.getType(), map.getValue());
			if (findCmp == null) {
				Application
						.getActiveApplication()
						.getOutput()
						.output("控件未找到：" + map.getType() + " " + map.getValue());
			}
		}
		return findCmp;
	}

	/* 根据查找关键字和关键字类型查找控件 */
	public static Component findComponent(Component c, SearchType type,
			Object info) {
		// 用tip查找
		Component findCmp = null;
		if (type.equals(SearchType.TIP)) {
			try {
				findCmp = new BasicFinder().find((Container) c, new TipMatcher(
						(String) info));
			} catch (ComponentNotFoundException
					| MultipleComponentsFoundException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
			}
		}

		if (type.equals(SearchType.TEXT)) {
			try {
				findCmp = new BasicFinder().find((Container) c,
						new TextMatcher((String) info));
			} catch (ComponentNotFoundException
					| MultipleComponentsFoundException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
			}
		}

		if (type.equals(SearchType.CLASS_NAME)) {
			try {
				findCmp = new BasicFinder().find((Container) c,
						new ClassMatcher((Class) info));
			} catch (ComponentNotFoundException
					| MultipleComponentsFoundException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
			}
		}
		return findCmp;
	}

	/* 根据各层级名称查找树节点，返回treepath */
	public static TreePath findTreePath(JTree tree, Object[] nodeInfo) {
		TreePath treePath = null;
		TreeModel model = tree.getModel();

		MutableTreeNode treeNode = (MutableTreeNode) model.getRoot();
		DefaultMutableTreeNode mutableTreeNode = null;
		for (int i = 0; i < treeNode.getChildCount(); i++) {
			mutableTreeNode = (DefaultMutableTreeNode) treeNode.getChildAt(i);
			System.out.println(mutableTreeNode.toString());
		}
		return treePath;
	}

	/* 暂时没有用到，用上面的方法finder */
	public static Component findComponentByTip(Container container, String tip) {
		Component c = null;
		if (container == null || tip == null)
			return c;
		for (int i = 0; i < container.getComponents().length; i++) {
			if (container.getComponent(i) instanceof JComponent
					&& tip.equals(((JComponent) container.getComponent(i))
							.getToolTipText())) {
				return container.getComponent(i);
			} else if (container.getComponent(i) instanceof Container) {
				c = findComponentByTip((Container) container.getComponent(i),
						tip);
				if (c != null)
					return c;
			}
		}
		return c;
	}

	/* JList 选择节点 */
	public static void selectListObject(JList jList, Object[] values) {
		if (jList == null || values == null || values.length == 0)
			return;
		try {
			selectListObject(jList, values[0]);
			Operation.robot.keyPress(KeyEvent.VK_CONTROL);
			for (int i = 1; i < values.length; i++) {
				selectListObject(jList, values[i]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Operation.robot.keyRelease(KeyEvent.VK_CONTROL);
		}
	}

	public static void selectListObject(JList jList, Object value) {
		if (jList == null || value == null)
			return;
		JListTester tester = new JListTester();
		try {
			if (value instanceof String) {
				tester.actionSelectItem(jList, (String) value);
			} 
			if (value instanceof Integer){
				tester.actionSelectIndex(jList, (int) value);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
